package com.myspring.sample.lookup;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;

@Configuration(proxyBeanMethods = false)
@ComponentScan()
public class LookupTest {
    /**
     * 参考1： https://blog.csdn.net/duxd185120/article/details/109125440
     * 参考2： https://www.baeldung.com/spring-lookup
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(LookupTest.class);
        context.refresh();
        
        System.out.println("user对象class类型：" + context.getBean(User.class));
        System.out.println("car对象：" + context.getBean(User.class).getCar().toString());
        System.out.println("car对象：" + context.getBean(User.class).getCar().toString());
 
    }
    
    //向容器注入car，并且scope是原型
    @Bean("car")
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)  //prototype scope是指每次get bean都返回一个新的实例，与之相反的是singleton scope
    public Car car(){
        //根据情况返回不同的实例
        return new Car("法拉利");
    }
 
    
}